﻿
@{
    ViewData["Title"] = "Form";
    Layout = "~/Views/Shared/_Index.cshtml";
}
    <link href="~/plugins/zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<div class="wrapper wrapper-content  animated fadeInRight">
    <div class="ibox col-lg-offset-3">
        <div class="ibox-content">
            <form class="form form-horizontal" id="myForm">
                <input type="hidden" name="id" id="id" value="@(ViewBag.Id)">
                <div class="form-group">
                    <label class="col-sm-3 control-label">部门名称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="department_name" id="department_name" value="" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">上级部门：</label>
                    <div class="col-sm-4">
                        <textarea id="parentDept" style="width:300px;height:50px;overflow:hidden;" textLabel="jasontext" type="text" readonly></textarea>
                        <input type="hidden" name="parent_id" id="parent_id" value="0" class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label required">排序：</label>
                    <div class="col-sm-4">
                        <input type="text" value="1" name="sort_num" id="sort_num" class="form-control" data-rule="required;integer; range[1~1000]">
                    </div>
                </div>
                <div class="hr-line-dashed"></div>
                <div class="footerbar">
                    <div class="col-sm-12 col-sm-offset-3">
                        <button class="btn btn-primary" type="submit">保存内容</button>
                        <button class="btn btn-white" type="button" onclick="cancel()">取消</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@section scripts{
    <script src="~/plugins/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script src="~/plugins/selectTree/MultipleTreeSelect.js"></script>
    <script>
        $(function () {
            $('#myForm').validator({
                stopOnError: false,
                timely: 2,
                theme: "yellow_right",
                valid: function (form) {
                    // 表单验证通过，提交表单
                    jutils.ajaxPost('/admin/SysDepartment/Save', $(form).serialize(),
                        function (res) {
                            cancel();
                        });
                }
            });

            loadData();
        });
        function loadData() {
            var id = jutils.getQueryParam("id");
            jutils.ajaxGet('/admin/SysDepartment/getmodel', { id: id }, function (res) {
                if (id != '0') {
                    var model = res.data.model
                    $('#myForm').initFormData(model);
                    if (model.parent_id != 0) {
                        $('#parentDept').attr('checks', model.parent_id);
                    }
                }
                initSelectTree(res.data.depts);
            });

        }
        function initSelectTree(treeNodes) {
            var defaults = {
                callback: {
                    onCheck: zTreeOnCheck
                },
                chkStyle: "radio",
                zNodes: treeNodes,
                height: 333   // 默认自定义下拉框高度
            }

            $("#parentDept").drawMultipleTree(defaults);
        }
        function zTreeOnCheck(treeNodes) {
           // console.log(treeNodes);

            // alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
            //selectDepartments.length = 0;
            if (treeNodes.length > 0) {
                var parentId = treeNodes[0].id;
                $('#parent_id').val(parentId);
            } else {
                $('#parent_id').val(0);
            }

            //console.log(selectDepartments);
        };
        function cancel() {
            jutils.closeForm();
        }
    </script>
}

